package com.jyu.apts.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.jyu.apts.dto.QRCodeDto;
import com.jyu.apts.dto.ResultDto;
import com.jyu.apts.dto.TableDto;
import com.jyu.apts.dto.UserDto;
import com.jyu.apts.entity.QRCode;
import com.jyu.apts.entity.InfoTemplate;
import com.jyu.apts.entity.User;
import com.jyu.apts.entityEnum.QRCodeStatusEnum;
import com.jyu.apts.entityEnum.QRCodeTypeEnum;
import com.jyu.apts.service.ItemProcessRecordService;
import com.jyu.apts.service.QRCodeService;
import com.jyu.apts.service.InfoTemplateService;
import com.jyu.apts.service.UserService;

@RestController
public class NormalQRCodeController {

	@Autowired
	QRCodeService qrCodeService;
	@Autowired
	InfoTemplateService infoTemplateService;
	@Autowired
	UserService userService;
	@Autowired
	ItemProcessRecordService itemProcessRecordService;
	
	@RequiresPermissions("normalQRCode_html")
	@RequestMapping("/normalQRCode_html")
	public ModelAndView normalQRCode_html() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/normalQRCode.html");
		return mv;
	}
	
	@RequiresPermissions("normalQRCode_html")
	@RequestMapping("/addNormalQRCode")
	public ResultDto addNormalQRCode(String[] dataValues,long infoTemplateId) {
		InfoTemplate infoTemplate = infoTemplateService.getOne(infoTemplateId);
		if(infoTemplate == null) {
			return new ResultDto("信息模板不存在",null,2);
		}
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		QRCode normalQRCode = new QRCode();
		normalQRCode.setAuthor(user);
		normalQRCode.setCode(UUID.randomUUID().toString().replaceAll("-", ""));
		StringBuilder sb = new StringBuilder("");
		if(dataValues == null || dataValues.length == 0) {
			sb.append("#");
		}
		for(String dataValue : dataValues) {
			sb.append("#");
			sb.append(dataValue);
		}
		normalQRCode.setDataValues(sb.toString().substring(1));
		normalQRCode.setType(QRCodeTypeEnum.NORMAL.getType());
		normalQRCode.setInfoTemplate(infoTemplate);
		normalQRCode.setStatus(QRCodeStatusEnum.UNUSED.getType());
		qrCodeService.save(normalQRCode);
		return new ResultDto("添加成功",null,0);
	}
	
	@RequiresPermissions("normalQRCode_html")
	@RequestMapping("/delNormalQRCode")
	public ResultDto delQRCode(String ids) {
		List<Long> l_ids = new ArrayList<Long>();
		for(String id : ids.split(",")) {
			l_ids.add(new Long(id));
		}
		List<QRCode> normalQRCodes = qrCodeService.findAllById(l_ids);
		for(QRCode qrCode : normalQRCodes) {
			if(qrCode.getType()!=QRCodeTypeEnum.NORMAL.getType())
				return new ResultDto("存在非普通二维码",null,2);
			if(qrCode.getStatus()!=QRCodeStatusEnum.UNUSED.getType())
				return new ResultDto("存在使用中的二维码",null,2);
		}
		qrCodeService.delete(normalQRCodes);
		return new ResultDto("删除成功",null,0);
	}
	
	@RequiresPermissions("normalQRCode_html")
	@RequestMapping("/getNormalQRCodePage")
	public TableDto<QRCodeDto> getQRCodePage(Integer pageNumber,Integer pageSize,Integer status,Long organizationId,String search) {
		Page<QRCode> pageResult = qrCodeService.getQRCodePage(pageNumber, pageSize,QRCodeTypeEnum.NORMAL.getType(),status, organizationId, search);
		List<QRCode> normalQRCodes = pageResult.getContent();
		List<QRCodeDto> normalQRCodeDtos = new ArrayList<QRCodeDto>();
		normalQRCodes.forEach(normalQRCode->normalQRCodeDtos.add(new QRCodeDto().convert(normalQRCode)));
		return new TableDto<QRCodeDto>(normalQRCodeDtos,pageResult.getTotalElements());
	}
	
}
